package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.shopping.domain.chat.ImUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.service.IImUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户
 *
 * @author Mr.Zhou
 * @date 2024-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/imUser")
public class ImUserController extends BaseController {

    private final IImUserService imUserService;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("chat:imUser:list")
    @GetMapping("/list")
    public TableDataInfo<ImUser> list(ImUser imUser, PageQuery pageQuery) {
        QueryWrapper<ImUser> qw = new QueryWrapper<>();

        TableDataInfo<ImUser> td = PageUtils.buildDataInfo(imUserService.page(PageUtils.buildPage(),qw));
        List<ImUser> list = td.getRows();
        return td;
    }

    /**
     * 导出用户列表
     */
    @SaCheckPermission("chat:imUser:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImUser imUser, HttpServletResponse response) {
        List<ImUser> list = imUserService.list();
        ExcelUtil.exportExcel(list, "用户", ImUser.class, response);
    }

    /**
     * 获取用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("chat:imUser:query")
    @GetMapping("/{id}")
    public R<ImUser> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(imUserService.getById(id));
    }

    /**
     * 新增用户
     */
    @SaCheckPermission("chat:imUser:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImUser imUser) {
        return toAjax(imUserService.save(imUser));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("chat:imUser:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImUser imUser) {
        return toAjax(imUserService.updateById(imUser));
    }

    /**
     * 删除用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("chat:imUser:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(imUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
